<template>
  <div>
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      title="调度任务" @resportConf="resportConf"
    ></v-tableTitle>
    <GTable
      ref="table"
      api="/v1/edi/pljob//selectjobpage"
      :hasEdit="true"
      :hasCheck="true"
      :columnSource="baseColumns"
      :cbFun="cbFun"
      @pause="pause"
      @resume="resume"
      @delete="del"
      @edit="edit"
      @execute="execute"
      @setdialog="setdialog"
      @head-btn-click="newCreate"
      @selection-change="data => tableSelectData = data"
    />

    <el-dialog
      :title="dialogModule.title"
      :visible.sync="dialogModule.flag"
      center
      v-dialogDrag
      width="720px"
      @closed="closed"
    >
      <div v-tit="'调度任务信息'">
        <GForm
          ref="detail"
          label-width="130px"
          :config="formConfig"
          :rules="formRules"
          v-model="form"
        ></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTitle from "commons/TableTitle";

export default {
  name: "dispatch",
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: "",
        data: null,
        type: "",
      },
      form: {
        jobName: null,
        jobGroup: null,
        jobType: null,
        jobCron: null,
        targetApiId: null,
        statusField: null,
        successStatusValue: null,
        description: null,
      },
      statusList: [
        { label: "等待", value: "WAITING" },
        { label: "暂停", value: "PAUSED" },
        { label: "阻塞", value: "BLOCKED" },
        { label: "正在执行", value: "ACQUIRED" },
      ],
      jobTypeList: [
        { label: "自定义定时任务", value: "0" },
        { label: "api定时任务", value: "1" },
        { label: "insert定时任务", value: "2" },
      ],
      envList: [],
    };
  },
  computed: {
    btnConfig() {
      return [
        {
          label: "字段映射",
          invalid: "resportConf",
          disabled: true,
          // permi: ["timeTask:add"],
        },
        // {
        //   label: this.$t('common.delete'),
        //   disabled: true,
        //   invalid: 'del'
        // }
      ];
    },
    baseColumns() {
      return [
        {
          label: "任务名称",
          prop: "jobName",
          input: "input",
        },
        {
          label: "任务所在组",
          prop: "jobGroup",
          input: "input",
        },
        {
          label: "任务类型",
          prop: "jobType",
          input: "select",
          options: this.jobTypeList,
        },
        {
          label: "触发表达式",
          prop: "jobCron",
          input: "input",
        },
        {
          label: "状态",
          prop: "enable",
          input: "select",
          options: [
            { label: "是", value: "true" },
            { label: "否", value: "false" },
          ],
        },
        {
          label: "任务描述",
          prop: "description",
          input: "input",
        },
        {
          label: "操作",
          prop: "action",
          input: "action",
          width: 360,
          btnConfig: [
            {
              label: "停用",
              invalid: "pause",
              type: "warning",
              disabled: (data) => data.enable == "是",
            },
            {
              label: "启用",
              invalid: "resume",
              type: "info",
              disabled: (data) => data.enable == "否",
            },
            {
              label: "删除",
              invalid: "delete",
              type: "danger",
              // permi: ["timeTask:delete"],
              disabled: (data) => data.enable == "否",
            },
            {
              label: "修改",
              invalid: "edit",
              type: "success",
              // permi: ["timeTask:edit"],
              disabled: data => data.enable == '否'
            },
            {
              label: "执行",
              invalid: "execute",
              type: "primary",
              // permi: ["timeTask:execute"],
              disabled: (data) => data.enable == "是",
            },
          ],
          headBtn: {
            label: "新增",
            type: "primary",
          },
        },
      ];
    },
    formConfig() {
      return [
        {
          prop: "jobName",
          type: "input",
          label: "任务名称",
        },
        {
          prop: "jobGroup",
          type: "input",
          label: "任务分组",
        },
        {
          prop: "jobType",
          type: "select",
          label: "任务类型",
          options: this.jobTypeList,
        },
        {
          prop: "environmentId",
          type: "select",
          label: "应用环境",
          options: this.envList,
        },
        {
          prop: "requestDateType",
          type: "input",
          label: "数据发送类型",
        },
        {
          label: "触发表达式",
          prop: "jobCron",
          type: "input",
        },
        {
          label: "查询数据sql",
          prop: "querySql",
          type: "textarea",
        },
        {
          label: "调用目标apiId",
          prop: "targetApiId",
          type: "input",
        },
        {
          label: "是否执行成功字段",
          prop: "statusField",
          type: "input",
        },
        {
          label: "执行成功值",
          prop: "successStatusValue",
          type: "input",
        },
        {
          label: "更新状态字段",
          prop: "updcolumn",
          type: "input",
        },
        {
          label: "更新状态sql",
          prop: "updsql",
          type: "textarea",
        },
        {
          prop: "description",
          type: "textarea",
          label: "任务描述",
        },
      ];
    },
    formRules() {
      return {
        jobName: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: "blur",
          },
        ],
        jobGroup: [
          {
            required: true,
            message: "请输入任务分组",
            trigger: "blur",
          },
        ],
        jobType: [
          {
            required: true,
            message: "请选择任务类型",
            trigger: "change",
          },
        ],
        jobCron: [
          {
            required: true,
            message: "请输入触发表达式",
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    resportConf(data){
      if(data.length == 1){
        this.$router.push({path:'dispatch/matchup',query:data[0]})
      }else{
        this.$message({
          message: '请选择一条数据进行操作',
          type: 'error',
        });
      }
    },
    handleConfirm() {
      this.$refs.detail.validate().then((valid) => {
        const params = this._.cloneDeep(this.form);
        let promise;
        if (this.dialogModule.type == "edit") {
          promise = this.$post(`/v1/edi/pljob/updjobBase`, params);
        } else {
          promise = this.$post(`/v1/edi/pljob/addJob`, params);
        }
        promise.then((res) => {
          this.$message.success(res.returnMgr);
          this.getTableData();
          this.$refs.detail?.resetFields();
          this.dialogModule.flag = false;
        });
      });
    },
    newCreate() {
      this.dialogModule.title = "新增定时任务";
      this.dialogModule.type = "add";
      this.dialogModule.data = null;
      this.dialogModule.flag = true;
      this.$refs.detail?.resetFields();
    },
    pause(data) {
      this.$post("/v1/edi/pljob/disactiveJob", data).then((res) => {
        this.$message.success(res.returnMgr);
        this.getTableData();
      });
    },
    resume(data) {
      this.$post("/v1/edi/pljob/activeJob", data).then((res) => {
        this.$message.success(res.returnMgr);
        this.getTableData();
      });
    },
    del(data) {
      this.$post("/v1/edi/pljob/delJob", data).then((res) => {
        this.$message.success(res.returnMgr);
        this.getTableData();
      });
    },
    edit(data) {
      this.$router.push({ path: "dispatchInfo", query: data });
    },
    setdialog(data) {
      this.dialogModule.title = "修改调度任务";
      this.dialogModule.type = "edit";
      this.dialogModule.data = data;
      this.$refs.detail?.resetFields();
      this.dialogModule.flag = true;
      this.$nextTick(() => {
        this.form = data;
      });
    },
    execute(data) {
      this.$post("/v1/edi/pljob/executeJob", data).then((res) => {
        this.$message.success(res.returnMgr);
        this.getTableData();
      });
    },
    closed() {
      this.$refs.detail?.resetFields();
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total };
    },
    async getTableData() {
      this.$refs.table.getTableData();
    },
    getEnvList() {
      this.$post("/v1/plApplicationEnvironment/selectList", {
        pageNum: 1,
        pageSize: 999,
      }).then((res) => {
        const data = res.data.page.records;
        this.envList = data.map((d) => {
          d.label = d.environmentName;
          d.value = d.id;
          return d;
        });
      });
    },
  },
  mounted() {
    this.getEnvList();
  },
  components: {
    "v-tableTitle": tableTitle,
  },
};
</script>

<style scoped></style>
